package cn.iocoder.yudao.module.bpm.controller.admin.definition;

import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.service.definition.BpmFormService;
import cn.iocoder.yudao.module.bpm.util.DatasourceUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 动态表单")
@RestController
@RequestMapping("/bpm/form/data")
@Validated
public class BpmFormDataController {


    @Resource
    private BpmFormService bpmFormService;

    @GetMapping("/uuid")
    public CommonResult<String> uuid() {
        return CommonResult.success(IdUtil.getSnowflakeNextIdStr());
    }

    @GetMapping("/listByFk")
    public CommonResult<List<Entity>> listByFk(String fk, String tableName) {
        return CommonResult.success(bpmFormService.listByFk(fk, tableName));
    }


    @PostMapping("/create")
    public CommonResult<String> createForm(@RequestBody JSONObject formData) {

        return bpmFormService.createForm(formData);
    }

    @PostMapping("/update")
    public CommonResult<Boolean> updateForm(@RequestBody JSONObject formData) {

        return bpmFormService.updateForm(formData);
    }

    @GetMapping("/delete")
    public CommonResult<Boolean> deleteForm(@RequestParam("id") String id, @RequestParam("tableName") String tableName) {
        DataSource master = DatasourceUtil.getDataSource("master");
        Entity where = Entity.create(tableName);
        //获取表里所有字段
        Table tableMeta = MetaUtil.getTableMeta(master, tableName);
        Collection<Column> columns = tableMeta.getColumns();
        Optional<Column> pk = columns.stream().filter(Column::isPk).findFirst();
        if (pk.isPresent()) {
            where.set(pk.get().getName(), id);
        }
        try {
            Db.use().del(where);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(500, "表单数据删除失败" + e.getMessage());
        }
        return success(true);
    }

    @GetMapping("/get")
    public CommonResult<Object> getForm(@RequestParam("id") String id, @RequestParam("tableName") String tableName) {

        return success(bpmFormService.getById(id, tableName));
    }
    @GetMapping("/get/pid")
    public CommonResult<Object> getByPid(@RequestParam("pid") String pid, @RequestParam("tableName") String tableName) {

        return success(bpmFormService.getByPId(pid, tableName));
    }

    @GetMapping("/list")
    public CommonResult<List<Entity>> list(String tableName) {
        return CommonResult.success(bpmFormService.list(tableName));
    }


}
